package com.whale.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.whale.constants.MenuGroupConstants;
import com.whale.kernel.domain.HtmlPage;
import com.whale.kernel.model.pagination.PagedList;
import com.whale.kernel.model.pagination.Step;

@Controller
@RequestMapping(value = "/news")
public class NewsController extends AbstractController {

	@RequestMapping(method = RequestMethod.GET)
	public String getListNewsgroup(HttpServletRequest request, Model model) {
		populateNavigation(model, "/news", MenuGroupConstants.USER_MENU_GROUP_ID);
		Step currentStep = getCurrentPageStep(request);
		PagedList<HtmlPage> pagedList = this.htmlPageService.findSubMenuPagesUnderTopMenu("/news", currentStep);
		model.addAttribute("childrenPages", pagedList.getResult());
		populatePagination("/news", request, model, currentStep, pagedList.getTotal());
		return "news/listNewsgroup";
	}

	@RequestMapping(value = "{newsgroupId}", method = RequestMethod.GET)
	public String getNewsgroup(@PathVariable String newsgroupId,
			HttpServletRequest request, Model model) {
		populateSubNavigation(model, "/news", "/news/" + newsgroupId, MenuGroupConstants.USER_MENU_GROUP_ID);
		Step currentStep = getCurrentPageStep(request);
		PagedList<HtmlPage> pagedList = this.htmlPageService.findHtmlPages("/news/" + newsgroupId, currentStep);
		model.addAttribute("childrenPages", pagedList.getResult());
		populatePagination("/news/" + newsgroupId, request, model, currentStep, pagedList.getTotal());
		return "news/newsgroup";
	}

	@RequestMapping(value = "{newsgroupId}/{newsId}", method = RequestMethod.GET)
	public String getNews(@PathVariable String newsgroupId,
			@PathVariable String newsId, Model model) {
		populateSubNavigation(model, "/news", "/news/" + newsgroupId, MenuGroupConstants.USER_MENU_GROUP_ID);
		model.addAttribute("htmlPage", this.htmlPageService.findHtmlPage(
				"/news/" + newsgroupId, newsId));
		return "news/newsView";
	}
}
